/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Buslog;

import ActiveRecord.Newdb.Db;
import ActiveRecord.UserActiveRecord;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author degrave
 */
public class SuperUser extends User {

    public SuperUser(int id, String name, String password, int per, int tableId) {
        super(id, name, password, per, tableId);
        //permission = 2;
    }

    public void addEvent(int newid, String newdate, String newtime, String newtext) {
        Event event = new Event();
        event.id = newid;
        event.date = newdate;
        event.time = newtime;
        event.text = newtext;
        event.ownerCalendarId = this.tableId;
        event.pushToDB();
    }

    public void pushToDB() {
        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            int my1 = this.id;
            String my2 = this.name;
            String my3 = this.password;
            int my4 = 2;
            int my5 = this.tableId;
            s.executeUpdate("insert into USERS(id, name, password, permission, tableid)"
                    + " values(" + my1 + ",'" + my2 + "','" + my3 + "'," + my4 + "," + my5 + ")");
            System.out.println("Inserted user to user table");
        } catch (SQLException ex) {
            System.out.println("Cant insert user to user table");
            Logger.getLogger(UserActiveRecord.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void removeFromDB() {
        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            s.executeUpdate("delete from USERS where id = " + id);
            System.out.println("User removed from user table");
        } catch (SQLException ex) {
            System.out.println("Cant remove user from user table");
            Logger.getLogger(UserActiveRecord.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getMaxId() {
        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            ResultSet rs = s.executeQuery("select MAX(id) from USERS");
            if (rs.next()) {
                return rs.getInt(1);
            } else {
                return 0;
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserActiveRecord.class.getName()).log(Level.SEVERE, null, ex);
            return 0;
        }
    }

    public int getMaxTableId() {
        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            ResultSet rs = s.executeQuery("select MAX(tableId) from USERS");
            if (rs.next()) {
                return rs.getInt(1);
            } else {
                return 0;
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserActiveRecord.class.getName()).log(Level.SEVERE, null, ex);
            return 0;
        }
    }

    public static SuperUser findUser(String name) {

        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            ResultSet rs = s.executeQuery("select * from USERS where NAME = '" + name + "'");
            if (rs.next()) {
                SuperUser man = new SuperUser(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4), rs.getInt(5));
                System.out.println("User finded from user table");
                return man;
            }
        } catch (SQLException ex) {
            System.out.println("Cant find user from user table");
            Logger.getLogger(ActiveRecord.UserActiveRecord.class.getName()).log(Level.SEVERE, null, ex);
        }
        return (new SuperUser(0, "", "", 0, 0));
    }

    public static SuperUser findUser(int id) {

        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            ResultSet rs = s.executeQuery("select * from USERS where tableId   = " + id);
            if (rs.next()) {
                SuperUser man = new SuperUser(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4), rs.getInt(5));
                System.out.println("User finded from user table");
                return man;
            }
        } catch (SQLException ex) {
            System.out.println("Cant find user from user table");
            Logger.getLogger(ActiveRecord.UserActiveRecord.class.getName()).log(Level.SEVERE, null, ex);
        }
        return (new SuperUser(0, "", "", 0, 0));
    }

    public static ResultSet findAllUsers() {

        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            ResultSet rs = s.executeQuery("SELECT * FROM USERS");
            System.out.println("Find all users from user table");
            return rs;
        } catch (SQLException ex) {
            System.out.println("Cant find all users from user table");
            Logger.getLogger(ActiveRecord.UserActiveRecord.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
